<?php
/**
 * CreateTime: 2022/5/10 17:23
 */

namespace App\Tools\Oss;


use OSS\OssClient;
use OSS\Core\OssException;

class OssTools
{
    /**
     * @param $securityToken '授权码'
     * @param $accessKeyId  '阿里云账号的临时accessKeyId'
     * @param $accessKeySecret '阿里云账号的临时accessKeySecret'
     * @param $endPoint 'OSS访问域名'
     * @param $bucket   'OSS存储空间'
     * @param $ossKey '对应OSS Object Key  <mediaId>'
     */
    public static function OSSUpload(
        $securityToken,
        $accessKeyId,
        $accessKeySecret,
        $endPoint,
        $bucket,
        $ossKey,
        $filePath){
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, "https://".$endPoint, false, $securityToken);
            // 使用STS临时授权上传文件。
            $ossClient->putObject($bucket, $ossKey, $filePath);
            return true;
        }catch (OssException $e) {
            return false;
        }
    }
}